# -*- coding: utf-8 -*-
import KBEngine
from KBEDebug import *
import d_items
import d_server
import d_task
import ComFun
import time
import random

class Warehouse:
    def __init__(self):
        pass

    def getItemNum(self, itemId):
        for i,itemData in enumerate(self.itemList):
            if itemId == itemData["itemId"]:
                return itemData["number"]
        return 0

    def addItem(self, itemId, number):
        for i,itemData in enumerate(self.itemList):
            if itemId == itemData["itemId"]:
                self.itemList[i]["number"] += number
                if self.itemList[i]["number"] <= 0:
                    del self.itemList[i]
                self.itemList = self.itemList
                return
        if number > 0:
            newItemData = {
                "itemId": itemId,
                "number": number
            }
            self.itemList.append(newItemData)
            self.itemList = self.itemList

    def initItem(self):
        for itemId in d_items.items.keys():
            if type(itemId) == int:
                itemData = {"itemId":itemId, "number":5}
                self.itemList.append(itemData)

    def haveProp(self, itemId, count, customArg):
        eval("self.%s(%d,%d,%d)" % (d_items.items[d_items.items[itemId]["prop_type"]]["value"], itemId, count, customArg))

    # 作用在植物上
    def prop_type_crop(self, itemId, count, customArg):
        for value in self.fieldList:
            if value["index"] == customArg:
                if self.getItemNum(itemId) < count:
                    return
                for prop in value["propList"]:
                    if prop["itemId"] == itemId:
                        prop["number"] += count
                        break
                else:
                    value["propList"].append({"itemId": itemId, "number": count})
                self.addItem(itemId, -count)
                self.fieldList = self.fieldList
                self.client.onHaveProp(itemId, count, customArg)

    # 作用在用户上
    def prop_type_user(self, itemId, count, customArg):
        if self.getItemNum(itemId) < count:
            return
        customArg = eval("self.%s(%d,%d)" % (d_items.items[d_items.items[itemId]["effect_type"]]["value"], itemId, count))
        if customArg == 0:
            return
        self.addItem(itemId, -count)
        self.task_daily_update(d_task.task["mode_prop"]["value"], count, itemId)
        self.client.onHaveProp(itemId, count, customArg)
    # 单次使用
    def prop_type_once(self, itemId, count, customArg):
        if self.getItemNum(itemId) < count:
            return
        customArg = eval("self.%s(%d,%d)" % (d_items.items[d_items.items[itemId]["effect_type"]]["value"], itemId, count))
        if customArg == 0:
            return
        self.addItem(itemId, -count)
        self.task_daily_update(d_task.task["mode_prop"]["value"], count, itemId)
        self.client.onHaveProp(itemId, count, customArg)

    # 影响时间
    def prop_effect_time(self, itemId):
        return eval("self.%s(%d)" % (d_items.items[d_items.items[itemId]["effect_supply"]]["value"], itemId))

    # 影响产量
    def prop_effect_yield(self, itemId):
        return eval("self.%s(%d)" % (d_items.items[d_items.items[itemId]["effect_supply"]]["value"], itemId))

    # 获得道具
    def prop_effect_prop(self, itemId, count):
        propId = eval("self.%s(%d)" % (d_items.items[d_items.items[itemId]["effect_supply"]]["value"], itemId))
        self.addItem(propId, count)
        return propId

    # 影响天气
    def prop_effect_weather(self, itemId, count):
        curTime = int(time.time())
        self.changeWeather(curTime)
        return curTime

    # 影响积分
    def prop_effect_score(self, itemId, count):
        number = eval("self.%s(%d)" % (d_items.items[d_items.items[itemId]["effect_supply"]]["value"], itemId))
        self.addScore(number * count, d_server.ScoreType.WareHouse)
        return number * count

    # 影响转盘体力
    def prop_effect_turntable(self, itemId, count):
        number = eval("self.%s(%d)" % (d_items.items[d_items.items[itemId]["effect_supply"]]["value"], itemId))
        self.addPhysicalValue(number * count)
        return number * count

    # 影响护盾时间
    def prop_effect_shield(self, itemId, count):
        if not self.isShieldOpened:
            self.promptClient("ShieldIsNotOpen")
            return 1
        number = eval("self.%s(%d)" % (d_items.items[d_items.items[itemId]["effect_supply"]]["value"], itemId))
        self.addShieldTime(number * count)
        return number *count

    # 固定值获取
    def prop_supply_fixed(self, itemId):
        return d_items.items[itemId]["effect"][0]

    # 千分率获取
    def prop_supply_permille(self, itemId):
        return d_items.items[itemId]["effect"][0]

    # 内容随机值获取
    def prop_supply_textrandom(self, itemId):
        length = len(d_items.items[itemId]["effect"])
        if 0 == length:
            return 0
        index = random.randint(0, length - 1)
        print("prop_supply_textrandom ==>> index : ", index)
        return d_items.items[itemId]["effect"][index]

    # 范围随机值获取
    def prop_supply_rangerandom(self, itemId):
        min = d_items.items[itemId]["effect"][0]
        max = d_items.items[itemId]["effect"][1]
        return  random.randint(min, max)
